package service

import (
	"fmt"
	"rongyy/src/models"
	"strconv"
)

type StatisService struct {
	d *models.StatisModel
}

func (ds *StatisService) Reg(begin string, end string) (int64, error) {
	sm := &models.StatisModel{}
	total, err := sm.Reg(begin, end)
	if err != nil {
		return 0, err
	}
	return total, nil
}

func (ds *StatisService) RegAll(begin string, end string, department string) ([]models.StatisRegAllModel, error) {
	sm := &models.StatisModel{}
	newArr := make([]models.StatisRegAllModel, 0)
	days, _ := strconv.Atoi(end[8:10])
	fmt.Println(days)
	month := end[0:7]
	var i int
	var date string
	for i = 1; i < days+1; i++ {

		if i < 10 {
			x := "0" + strconv.Itoa(i)
			date = month + "-" + x
		} else {
			date = month + "-" + strconv.Itoa(i)
		}
		d := &models.StatisRegAllModel{
			Date:  date,
			Count: 0,
		}
		newArr = append(newArr, *d)
	}

	data, err := sm.RegAll(begin, end, department)
	for x := 0; x < len(newArr); x++ {
		for y := 0; y < len(data); y++ {
			if newArr[x].Date == data[y].Date {
				newArr[x].Count = data[y].Count
			}
		}
	}
	if err != nil {
		return nil, err
	}
	return newArr, nil
}

func (ds *StatisService) PatAll(begin string, end string, department string) ([]models.StatisRegAllModel, error) {
	sm := &models.StatisModel{}
	newArr := make([]models.StatisRegAllModel, 0)
	days, _ := strconv.Atoi(end[8:10])
	fmt.Println(days)
	month := end[0:7]
	var i int
	var date string
	for i = 1; i < days+1; i++ {

		if i < 10 {
			x := "0" + strconv.Itoa(i)
			date = month + "-" + x
		} else {
			date = month + "-" + strconv.Itoa(i)
		}
		d := &models.StatisRegAllModel{
			Date:  date,
			Count: 0,
		}
		newArr = append(newArr, *d)
	}

	data, err := sm.PatAll(begin, end, department)
	for x := 0; x < len(newArr); x++ {
		for y := 0; y < len(data); y++ {
			if newArr[x].Date == data[y].Date {
				newArr[x].Count = data[y].Count
			}
		}
	}
	if err != nil {
		return nil, err
	}
	return newArr, nil
}

func (ds *StatisService) CardTotals(begin string, end string) (int64, error) {
	sm := &models.StatisModel{}
	data, err := sm.GetTotalPat(begin, end)

	if err != nil {
		return data, err
	}
	return data, nil
}

func (ds *StatisService) RegTotals(begin string, end string) (int64, error) {
	sm := &models.StatisModel{}
	data, err := sm.GetTotalReg(begin, end)

	if err != nil {
		return data, err
	}
	return data, nil
}

func (ds *StatisService) OpTotals(begin string, end string) (int64, error) {
	sm := &models.StatisModel{}
	data, err := sm.GetTotalOp(begin, end)

	if err != nil {
		return data, err
	}
	return data, nil
}
